package spring;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.io.*;
/*
 * PoiEditor.java
 *
 * Created on Jul 24, 2008, 2:03:41 PM
 */

/**
 *
 * @author spring
 */
public class PoiEditor extends javax.swing.JDialog {

    /** Creates new form PoiEditor */
    public PoiEditor(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        java.awt.Rectangle rectOwner, rectDialog;
        rectOwner = parent.getBounds();
        rectDialog = this.getBounds();
        setLocation(rectOwner.x + rectOwner.width /2 - rectDialog.width /2,
        rectOwner.y + rectOwner.height/2 - rectDialog.height /2);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        POIS = new javax.swing.JTabbedPane();
        LoadPoiSet = new javax.swing.JButton();
        SavePoiSet = new javax.swing.JButton();
        Cancel = new javax.swing.JButton();
        SaveAsPoiSet = new javax.swing.JButton();
        BuildPOISet = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        LoadPoiSet.setText("Load POI Set");
        LoadPoiSet.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LoadPoiSetActionPerformed(evt);
            }
        });

        SavePoiSet.setText("Save & Exit");
        SavePoiSet.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SavePoiSetActionPerformed(evt);
            }
        });

        Cancel.setText("Cancel");
        Cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelActionPerformed(evt);
            }
        });

        SaveAsPoiSet.setText("Save As & Exit");
        SaveAsPoiSet.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SaveAsPoiSetActionPerformed(evt);
            }
        });

        BuildPOISet.setText("Build POI Set");
        BuildPOISet.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BuildPOISetActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(POIS, javax.swing.GroupLayout.DEFAULT_SIZE, 491, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(LoadPoiSet)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(BuildPOISet)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(SavePoiSet)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(SaveAsPoiSet)
                        .addGap(18, 18, 18)
                        .addComponent(Cancel)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(POIS, javax.swing.GroupLayout.DEFAULT_SIZE, 244, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(LoadPoiSet)
                    .addComponent(SavePoiSet)
                    .addComponent(Cancel)
                    .addComponent(SaveAsPoiSet)
                    .addComponent(BuildPOISet))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void LoadPoiSetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_LoadPoiSetActionPerformed
        // TODO add your handling code here:
        javax.swing.JFileChooser fc = new javax.swing.JFileChooser(".//data");
        javax.swing.filechooser.FileFilter imageFilter = new javax.swing.filechooser.FileFilter() {

            public boolean accept(File file) {
                String name = file.getName().toLowerCase();
                return name.endsWith(".poi");
            }

            public String getDescription() {
                return "POI Files";
            }
        };
        fc.setFileFilter(imageFilter);
        int r = fc.showOpenDialog(this);
        if (r == javax.swing.JFileChooser.APPROVE_OPTION) {
            poifile = fc.getSelectedFile();
            int i = 0;
            try {
                BufferedReader poi = new BufferedReader(new FileReader(poifile));
                String tmpstr;
                while ((tmpstr = poi.readLine()) != null && i < 20) {
                    String[] parts = tmpstr.split(",", 7);
                    pd[i] = new POIDisplay();
                    pd[i].setXloc(new Double(parts[0]).doubleValue());
                    pd[i].setYloc(new Double(parts[1]).doubleValue());
                    pd[i].setColor(new Integer(parts[2]).intValue(),
                            new Integer(parts[3]).intValue(), new Integer(parts[4]).intValue());
                    pd[i].setName(parts[5]);
                    String[] sv = parts[6].split(",");
                    for (int j = 0; j < sv.length - 1; j += 2) {
                        pd[i].setTerm(sv[j] + "," + sv[j + 1]);
                    }
                    POIS.addTab(parts[5], pd[i]);
                    POIS.setBackgroundAt(i, pd[i].getColor());
                    i++;
                }
                npoi = i;
                poi.close();
                this.pack();
            } catch (FileNotFoundException fne) {
                System.out.println("LoadPOIERROR: " + fne.getMessage());
                fne.printStackTrace();
            } catch (IOException ioe) {
                System.out.println("LoadPOIERROR: " + ioe.getMessage());
                ioe.printStackTrace();
            }
        }//end if

    }//GEN-LAST:event_LoadPoiSetActionPerformed

    private void CancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelActionPerformed
        // TODO add your handling code here:
        this.dispose();
}//GEN-LAST:event_CancelActionPerformed

    private void SaveAsPoiSetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SaveAsPoiSetActionPerformed
        // TODO add your handling code here:
        javax.swing.JFileChooser fc = new javax.swing.JFileChooser(".//data");
        javax.swing.filechooser.FileFilter imageFilter = new javax.swing.filechooser.FileFilter() {

            public boolean accept(File file) {
                String name = file.getName().toLowerCase();
                return name.endsWith(".poi");
            }

            public String getDescription() {
                return "POI Files";
            }
        };
        fc.setFileFilter(imageFilter);
        int r = fc.showSaveDialog(this);
        if (r == javax.swing.JFileChooser.APPROVE_OPTION) {
            File poifile = fc.getSelectedFile();
            int i = 0;
            try {
                java.text.DecimalFormat formval = new java.text.DecimalFormat("#0.00");
                BufferedWriter poi = new BufferedWriter(new FileWriter(poifile));
                String tmpstr = "";
                for (int n = 0; n < POIS.getComponentCount(); n++) {
                    POIDisplay pd = (POIDisplay) POIS.getComponent(n);
                    tmpstr = formval.format(pd.getXloc()) + "," +
                            formval.format(pd.getYloc()) + "," +
                            pd.getColor().getRed() + "," +
                            pd.getColor().getGreen() + "," +
                            pd.getColor().getBlue() + "," +
                            pd.getName() + ",";
                    String tmpstr2;
                    while ((tmpstr2 = pd.getTerm()) != null) {
                        tmpstr += tmpstr2 + ",";
                    }
                    poi.write(tmpstr + "\n");
                }
                poi.close();
                this.pack();
            } catch (FileNotFoundException fne) {
                System.out.println("LoadPOIERROR: " + fne.getMessage());
                fne.printStackTrace();
            } catch (IOException ioe) {
                System.out.println("LoadPOIERROR: " + ioe.getMessage());
                ioe.printStackTrace();
            }
        }//end if
this.dispose();
}//GEN-LAST:event_SaveAsPoiSetActionPerformed

    private void BuildPOISetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BuildPOISetActionPerformed
        // TODO add your handling code here:
        javax.swing.JFileChooser fc = new javax.swing.JFileChooser(".//data");
        javax.swing.filechooser.FileFilter imageFilter = new javax.swing.filechooser.FileFilter() {

            public boolean accept(File file) {
                String name = file.getName().toLowerCase();
                return name.endsWith(".poiset");
            }

            public String getDescription() {
                return "POI Files";
            }
        };
        fc.setFileFilter(imageFilter);
        int r = fc.showOpenDialog(this);
        if (r == javax.swing.JFileChooser.APPROVE_OPTION) {
            File poisetfile = fc.getSelectedFile();
            int i = 0;
            String tmpstr, tmpstr2;
            try {
                BufferedReader poi = new BufferedReader(new FileReader(poisetfile));
                int icandidates = new Integer(poi.readLine()).intValue();
                tmpstr = poi.readLine();
                for (int j = 0; j < icandidates; j++) {
                    POIBuilder pb = new POIBuilder(this, true);
                    while (!(tmpstr = poi.readLine()).contentEquals("TERMS")) {

                        pb.setDoc(tmpstr);
                    }
                    while (poi.ready() && !(tmpstr = poi.readLine()).contentEquals("DOCUMENTS")) {
                        String[] parts = tmpstr.split(",");
                        pb.setTerm(parts[0]+","+(new Double(parts[1]).doubleValue())/10.0);
                    }
                    pb.setName("Name?");
                    pb.setCLabel("Cluster " + (j + 1) + " of " + icandidates + " possible clusters.");
                    pb.setPLabel("POI " + (i + 1) + " of 20 Max POI.");
                    pb.pack();
                    pb.setpos();
                    pb.setVisible(true);
                    if (pb.MakePOI()) {
                        pd[i] = new POIDisplay();
                        pd[i].setXloc(pb.getXloc());
                        pd[i].setYloc(pb.getYloc());
                        pd[i].setColor(pb.getColor().getRed(),
                                pb.getColor().getGreen(),
                                pb.getColor().getBlue());
                        pd[i].setName(pb.getName());
                        while ((tmpstr2 = pb.getTerm()) != null) {
                            pd[i].setTerm(tmpstr2);
                        }
                        POIS.addTab(pb.getName(), pd[i]);
                        POIS.setBackgroundAt(i, pd[i].getColor());
                        i++;
                    }
                    pb.dispose();
                    if (pb.AbortProcess()) break;
                }
                npoi = i;
                poi.close();
                this.pack();
            } catch (FileNotFoundException fne) {
                System.out.println("LoadPOIERROR: " + fne.getMessage());
                fne.printStackTrace();
            } catch (IOException ioe) {
                System.out.println("LoadPOIERROR: " + ioe.getMessage());
                ioe.printStackTrace();
            }
        }//end if

    }//GEN-LAST:event_BuildPOISetActionPerformed

    private void SavePoiSetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SavePoiSetActionPerformed
        // TODO add your handling code here:
       if (poifile != null) {
            int i = 0;
            try {
                java.text.DecimalFormat formval = new java.text.DecimalFormat("#0.00");
                BufferedWriter poi = new BufferedWriter(new FileWriter(poifile));
                String tmpstr = "";
                for (int n = 0; n < POIS.getComponentCount(); n++) {
                    POIDisplay pd = (POIDisplay) POIS.getComponent(n);
                    tmpstr = formval.format(pd.getXloc()) + "," +
                            formval.format(pd.getYloc()) + "," +
                            pd.getColor().getRed() + "," +
                            pd.getColor().getGreen() + "," +
                            pd.getColor().getBlue() + "," +
                            pd.getName() + ",";
                    String tmpstr2;
                    while ((tmpstr2 = pd.getTerm()) != null) {
                        tmpstr += tmpstr2 + ",";
                    }
                    poi.write(tmpstr + "\n");
                }
                poi.close();
                this.pack();
            } catch (FileNotFoundException fne) {
                System.out.println("LoadPOIERROR: " + fne.getMessage());
                fne.printStackTrace();
            } catch (IOException ioe) {
                System.out.println("LoadPOIERROR: " + ioe.getMessage());
                ioe.printStackTrace();
            }
       }
       this.dispose();
    }//GEN-LAST:event_SavePoiSetActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                PoiEditor dialog = new PoiEditor(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    private POIDisplay[] pd = new POIDisplay[20];
    private int npoi = 0;
    private File poifile = null;

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BuildPOISet;
    private javax.swing.JButton Cancel;
    private javax.swing.JButton LoadPoiSet;
    private javax.swing.JTabbedPane POIS;
    private javax.swing.JButton SaveAsPoiSet;
    private javax.swing.JButton SavePoiSet;
    // End of variables declaration//GEN-END:variables
}
